<template>
  <div class="app-container home">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">

      <el-form-item label="订单时间" prop="orderTime">
        <el-date-picker clearable
          v-model="queryParams.orderTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择订单时间">
        </el-date-picker>
      </el-form-item>


      <el-form-item label="订单状态" prop="orderStatusId">
    <el-select
      v-model="queryParams.orderStatusId"
      placeholder="请选择订单状态"
      clearable
      @change="handleQuery"
    >
      <el-option
        v-for="item in statusOptions"
        :key="item.value"
        :label="item.label"
        :value="item.value"
      />
    </el-select>
</el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

      <EChartComponent :xAxisData="xAxisData" :seriesData="seriesData" />
      <div v-if="loading">Loading...</div>
  </div>
</template>

<script>
import { listMerchant_order } from "@/api/system/merchant_order";
import EChartComponent from './EChartComponent.vue';


export default {
  components: {
    EChartComponent,
  },
  name: "Index",
  data() {
    return {
       // 遮罩层
       loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedTbCustomerOrderDetail: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 订单管理表格数据
      merchant_orderList: [],
      // 商家-顾客：订单详情

      tbCustomerOrderDetailList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      statusOptions: [
      { label: '未支付', value: 5 },
      { label: '已支付', value: 6 },
      // { label: '已过期', value: 5 },
      // { label: '已取消', value: 5 },
      // { label: '已完成', value: 6 }
    ],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 100000,
        customerId: null,
        customerName: null,
        orderTime: null,
        orderPrice: null,
        orderPoint: null,
        orderStatusId: 6,
        orderComment: null,
        orderCallnum: null,
        merchantId: null
      },
      loading: false,
      merchant_orderList: [],
      total: 0,
      xAxisData: [], // 用于 ECharts 的 x 轴数据
      seriesData: [], // 用于 ECharts 的系列数据
      merchant_orderList:{},//订单列表
      // 版本号
      version: "3.6.4",
    };
  },
  created() {
    this.getList();
  },
  methods: {
     /** 查询订单管理列表 */
     getList() {
  this.loading = true;
  listMerchant_order(this.queryParams).then(response => {
    this.merchant_orderList = response.rows;
    this.total = response.total;

    // 提取并整理数据
    const salesData = this.merchant_orderList.map(order => ({
      time: order.orderTime.split("T")[0], // 提取日期
      price: order.orderPrice,
    }));

    const salesByDate = salesData.reduce((acc, { time, price }) => {
      acc[time] = (acc[time] || 0) + price; // 累加销售额
      return acc;
    }, {});

    // 设置 x 轴和系列数据
    this.xAxisData = Object.keys(salesByDate);
    this.seriesData = Object.values(salesByDate);

    this.loading = false;
  }).catch(error => {
    console.error(error);
    this.loading = false;
  });
},
    goTarget(href) {
      window.open(href, "_blank");
    },
     // 取消按钮
     cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        orderNumber: null,
        customerId: null,
        orderTime: null,
        orderPrice: null,
        orderPoint: null,
        orderStatusId: null,
        orderComment: null,
        orderCallnum: null,
        merchantId: null
      };
      this.tbCustomerOrderDetailList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.orderNumber)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加订单管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const orderNumber = row.orderNumber || this.ids
      getMerchant_order(orderNumber).then(response => {
        this.form = response.data;
        this.tbCustomerOrderDetailList = response.data.tbCustomerOrderDetailList;
        this.open = true;
        this.title = "修改订单管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.tbCustomerOrderDetailList = this.tbCustomerOrderDetailList;
          if (this.form.orderNumber != null) {
            updateMerchant_order(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addMerchant_order(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const orderNumbers = row.orderNumber || this.ids;
      this.$modal.confirm('是否确认删除订单管理编号为"' + orderNumbers + '"的数据项？').then(function() {
        return delMerchant_order(orderNumbers);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
	/** 商家-顾客：订单详情

    rowTbCustomerOrderDetailIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 商家-顾客：订单详情  */
    handleAddTbCustomerOrderDetail() {
      let obj = {};
      obj.productName = "";
      obj.productNum = "";
      this.tbCustomerOrderDetailList.push(obj);
    },


    /** 复选框选中数据 */
    handleTbCustomerOrderDetailSelectionChange(selection) {
      this.checkedTbCustomerOrderDetail = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/merchant_order/export', {
        ...this.queryParams
      }, `merchant_order_${new Date().getTime()}.xlsx`)
    }
  },
};
</script>

<style scoped lang="scss">
.home {
  blockquote {
    padding: 10px 20px;
    margin: 0 0 20px;
    font-size: 17.5px;
    border-left: 5px solid #eee;
  }
  hr {
    margin-top: 20px;
    margin-bottom: 20px;
    border: 0;
    border-top: 1px solid #eee;
  }
  .col-item {
    margin-bottom: 20px;
  }

  ul {
    padding: 0;
    margin: 0;
  }

  font-family: "open sans", "Helvetica Neue", Helvetica, Arial, sans-serif;
  font-size: 13px;
  color: #676a6c;
  overflow-x: hidden;

  ul {
    list-style-type: none;
  }

  h4 {
    margin-top: 0px;
  }

  h2 {
    margin-top: 10px;
    font-size: 26px;
    font-weight: 100;
  }

  p {
    margin-top: 10px;

    b {
      font-weight: 700;
    }
  }

  .update-log {
    ol {
      display: block;
      list-style-type: decimal;
      margin-block-start: 1em;
      margin-block-end: 1em;
      margin-inline-start: 0;
      margin-inline-end: 0;
      padding-inline-start: 40px;
    }
  }
}
</style>

